#!/bin/bash

# set -x

GITFILE="modlist-sorted.txt"
SOURCEDIR="./"

DOWNLOADED="Downloaded ... "
DOWNLOADFAILED="Download failed for ... "
LINE="_/_/_/_/_/_/_/_/_/"
LOGFILE="log.log"
NOTACCESSIBLE="The directory $SOURCEDIR  is not accessible !"
NOTREADABLE="The file $GITFILE is not readable !"
SPACE=" "

NbProcessed=0

echo $SPACE > $LOGFILE

if [ -d $SOURCEDIR ]; then
    if [ -r $GITFILE ]; then
	for i in `cat $GITFILE`; do
	    ((NbProcessed=NbProcessed+1))
	    echo $SPACE >> $LOGFILE
	    echo $NbProcessed >> $LOGFILE
	    echo $SPACE >> $LOGFILE
	    git clone $i
	    if [ $? == 128 ] ; then
		echo $SPACE >> $LOGFILE
		echo $LINE >> $LOGFILE
		echo $SPACE >> $LOGFILE
		echo $DOWNLOADFAILED $i >> $LOGFILE
		echo $SPACE >> $LOGFILE
		echo $LINE >> $LOGFILE
		echo $SPACE >> $LOGFILE
	    else
		echo $SPACE >> $LOGFILE
		echo $LINE >> $LOGFILE
		echo $SPACE >> $LOGFILE
		echo $DOWNLOADED $i >> $LOGFILE
		echo $SPACE >> $LOGFILE
		echo $LINE >> $LOGFILE
		echo $SPACE >> $LOGFILE
	    fi
	done
    else
	echo $NOTREADABLE
    fi
else
    echo $NOTACCESSIBLE
fi
